<?php
namespace bee;

interface Isingleton
{ public static function getInstance();
}

abstract class singleton extends object implements Isingleton
{ static protected $__instance;
  protected $__class;

  protected function __clone()
  { throw new \Exception("Singleton class can't be cloned", 100);
  }
  protected function __construct() {;}

  public static function getInstance()
  {
    $classname = get_called_class();
  	if(!(static::$__instance instanceof self))
    {
      static::$__instance = new $classname;
    }
    return static::$__instance;
  }

  public function __call($name, $arguments)
  { //echo("<br />__call: " . " $name" . " / " . print_r($this->__class, true) . " :/: " . print_r($arguments, true));
    if(!$this->__class) debug_print_backtrace();
    try
    {
      return call_user_func_array(array($this->__class, $name), $arguments);
    } catch (Exception $ex)
    { //echo "<br />\nCLASS: " . print_r($this->__class, true) . "<br />\nNAME: " . $name . "<br />\nARGUMENTS: " . print_r($arguments, true);
    }
    //return call_user_func_array(array(static::$__instance, $name), $arguments);
  }

  public function setInstance($object)
  {
    $className = get_called_class();
    $className::$__instance = $object;
  }
}